import random
import threading
import time
from datetime import datetime

import akshare as ak
import MysqlHelper as sqlHelper

import Shareholder

if __name__=="__main__":
    # intVal=-1
    # try:
    #     val="-5952187a"
    #     intVal=int(val)
    # except ValueError as e:
    #     intVal=-1
    # print(intVal)

    shareholder=Shareholder.Shareholder()
    shareholder.top10();

# 十大股东及变动情况
# date:2024-03-31   2023-06-30  2023-09-30   2023-12-31
# stock_gdfx_free_top_10_em_df=None
# shareholder=""
# try:
#     stock_gdfx_free_top_10_em_df = ak.stock_gdfx_top_10_em(symbol="SZ000038", date="20240630")
#     # print(stock_gdfx_free_top_10_em_df)
# except Exception as e:
#     stock_gdfx_free_top_10_em_df = ak.stock_gdfx_top_10_em(symbol="SZ000038", date="20240331")
#     # print("except")
#     # print(stock_gdfx_free_top_10_em_df)
# # print("-----")
# for row in stock_gdfx_free_top_10_em_df.iterrows():
#     if row[1].get('股东名称')=="香港中央结算有限公司" or row[1].get('股东名称')[:6] == "全国社保基金":
#         shareholder+="\n\t"+row[1].get('股东名称') + ">" + row[1].get('增减')
# if shareholder!="":
#     shareholder="sh600690"+shareholder
# print(shareholder)
#
# sql = "select * from base_stock"
# db = sqlHelper.MysqlHelper()
# dbstock = db.select(sql)
# for item in dbstock:
#     code = item[1][7:] + item[1][:6]
#     print(code)
#     stock_gdfx_free_top_10_em_df = None
#     try:
#         stock_gdfx_free_top_10_em_df = ak.stock_gdfx_top_10_em(symbol=code, date="20240630")
#         # print(stock_gdfx_free_top_10_em_df)
#     except Exception as e:
#         stock_gdfx_free_top_10_em_df = ak.stock_gdfx_top_10_em(symbol=code, date="20240331")
#     shareholder = ""
#     if not stock_gdfx_free_top_10_em_df.empty:
#         for row in stock_gdfx_free_top_10_em_df.iterrows():
#             if row[1].get('股东名称')[:6] == "全国社保基金": #row[1].get('股东名称') == "香港中央结算有限公司" or
#                 shareholder += "\n\t" + row[1].get('股东名称') + ">" + row[1].get('增减')
#     if shareholder != "":
#         shareholder = code + shareholder
#         print(shareholder)

# def threadTest(name):
#     speed=random.randint(1,5)
#     print("%s 执行%d秒" % (name,speed))
#     print(threading.get_ident())
#     time.sleep(speed)
#     print("%s结束" % name)
#
# t1 = threading.Thread(target=threadTest, name="t1", args=("obj1",))
# t2 = threading.Thread(target=threadTest, args=("obj2",))
# t1.start()
# t2.start()
# print("活动的线程数：",threading.active_count(),"分别是：")
# print(threading.enumerate())
# t1.join()
# t2.join()
# print("主线程对象",threading.main_thread())
#
# lock=threading.Lock()
# flag=lock.acquire(blocking=True)
# print("flag=",flag,"; locked=",lock.locked())
# flag=lock.acquire(blocking=True)
# print("flag1=",flag,"; locked1=",lock.locked())
# lock.release()
#
# print("运行结束")
# 测试插入
# insert into ${tableName} (trade_date,open_price,high_price,low_price,close_price,change_price,pct_chg,vol,amount,updateTime,pre_close)
#         values (#{dailyDataPO.tradeDate},#{dailyDataPO.openPrice},#{dailyDataPO.highPrice},#{dailyDataPO.lowPrice},#{dailyDataPO.closePrice},#{dailyDataPO.changePrice},#{dailyDataPO.pctChg},#{dailyDataPO.vol},#{dailyDataPO.amount},SYSDATE(),${dailyDataPO.preClose})
#         ON DUPLICATE KEY UPDATE open_price=#{dailyDataPO.openPrice},high_price=#{dailyDataPO.highPrice},low_price=#{dailyDataPO.lowPrice},close_price=#{dailyDataPO.closePrice}
#         ,change_price=#{dailyDataPO.changePrice},pct_chg=#{dailyDataPO.pctChg},vol=#{dailyDataPO.vol},amount=#{dailyDataPO.amount},updateTime=SYSDATE(),pre_close=${dailyDataPO.preClose};

# 添加新GP
# import AppendNewStock
# appendNewStock=AppendNewStock.AppendNewStock()
# appendNewStock.run()

# 日期格式化
# date1 = datetime.strptime("2024-04-08", "%Y-%m-%d")
# print(date1.strftime("%Y%m%d"))

# 批量删除
# list1 = [('c', 'ccc'), ('d', 'ddd'), ('e', 'eee')]
# db = sqlHelper.MysqlHelper()
# sql = "insert into testTable (`title`,`content`) values (%s,%s)"
# db.execute_many(sql, list1)

# sql="select * from base_stock"
# db = sqlHelper.MysqlHelper()
# dbstock=db.select(sql)
# # 股票列表-上证 上市日期等
# newStock=ak.stock_info_sh_name_code()
# for row in newStock.iterrows():
#     code=row["证券代码"]
#      # filter(lambda item:item==code,dbstock)
#     count=sum(1 for item in dbstock if item["stock_code"]==code)
#     if count==0:
#         # 不存在
#         datetime.strptime(row["上市日期"])
# # 股票列表-A股
# ak.stock_info_a_code_name()
# # 股票列表-深证 上市日期
# ak.stock_info_sz_name_code()
# # 历史行情数据
# stockes = ak.stock_zh_a_hist(symbol="000001", period="daily", start_date="", end_date="", adjust="qfq")

# stock_list = ak.stock_zh_a_gdhs(symbol="20231231")
# print("条数：%s" % stock_list.count())
# for index, row in stock_list.iterrows():
#     print("%s--%s,股东数：%s" % (row["代码"], row["名称"], row["股东户数-本次"]))

# db=sqlHelper.MysqlHelper()
# updateBaseInfoSql="update base_stock set stock_name=%s where stock_code =%s"
# db.execute_sqlArgs(updateBaseInfoSql,"平安银行1","000001")

# stock_a_indicator_lg_df = ak.stock_a_indicator_lg(symbol="000001")
# print(stock_a_indicator_lg_df)

# 更新名称
# stock_list = ak.stock_zh_a_spot_em()
# print("条数：%s" % stock_list.count())
# updateBaseInfoSql = "update base_stock set stock_name=%s where stock_code =%s"
# db = sqlHelper.MysqlHelper()
# for index, row in stock_list.iterrows():
#     code2 = row["代码"][0:2]
#     if code2 == "60" or code2 == "00":
#         print("%s--%s" % (row["代码"], row["名称"]))
#         db.execute_sqlArgs(updateBaseInfoSql, row["名称"], row["代码"])


# sql测试
# sql = "select * from base_heed where heed_id=%s"
# db=sqlHelper.MysqlHelper()
# result = db.select(sql,"2")
# print(result)
